package com.topisv.tms.web.edi;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.EdiGoodsSku;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.EdiGoodsSkuService;
import com.topisv.tms.service.edi.EdiSourceService;
import com.topisv.tms.service.vo.edi.TMSSource;
import com.topisv.tms.utils.TimeUtil;
import com.topisv.tms.utils.date.TopIsvDateUtils;
import com.topisv.tms.utils.myjson.MyJsDateJsonValueProcessor;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping(value = "soce")
public class SourceController {

	private static final Logger log = LoggerFactory.getLogger(SourceController.class);

	
	@Autowired
	private EdiSourceService ediSourceService;
	@Autowired
	private EdiGoodsSkuService ediGoodsSkuService;
	@Autowired
	private CustomersService customersService;
	
	@RequestMapping(value = "queryOrder", method = RequestMethod.POST)
	public void queryOrder(@RequestParam(value = "customerCode") String customerCode,
			@RequestParam(value = "fromDate") String from,
			@RequestParam(value = "endDate") String end,
			@RequestParam(value = "type") String type,
			HttpServletResponse response){
		PrintWriter out = null;
		JSONObject json = null;
		Date fromDate=null;
		Date endDate=null;
		long count=0;
		try {
			log.info("customerCode=="+customerCode);
			log.info("fromDate=="+from);
			log.info("endDate=="+end);
			log.info("type=="+type);
			out = response.getWriter();
			json = new JSONObject();
			Customers customers=customersService.findByCustomerCode(customerCode);
			if(customers==null){
				json.put("status", "-1");
				json.put("content", "客户编码错误");
				out.print(json.toString());
				return;
			}
			if(from.length()==10){
				fromDate=TimeUtil.getDate(from);
			}else if(from.length()==19){
				fromDate=TimeUtil.getDateMMSS(from);
			}else{
				json.put("status", "-1");
				json.put("content", "起始时间格式不对"+from);
				out.print(json.toString());
				return;
			}
			if(end.length()==10){
				endDate=TimeUtil.getDate(end);
			}else if(end.length()==19){
				endDate=TimeUtil.getDateMMSS(end);
			}else{
				json.put("status", "-1");
				json.put("content", "终止时间格式不对"+end);
				out.print(json.toString());
				return;
			}
			endDate=TopIsvDateUtils.addHours(endDate, -14);
			fromDate=TopIsvDateUtils.addHours(fromDate, -14);
			if("single".equals(type)){
				count=ediSourceService.findByCrtimeAdtime(fromDate, endDate, type);
			}else if("multiple".equals(type)){
				count=ediSourceService.findByCrtimeAdtime(fromDate, endDate, type);
			}else if("all".equals(type)){
				count=ediSourceService.findByCrtimeAdtime(fromDate, endDate);
			}else{
				json.put("status", "-1");
				json.put("content", "订单类型参数错误"+type);
				out.print(json.toString());
				return;
			}
			json.put("status", "1");
			json.put("content", count);
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
		//log.info("queryOrder=="+json.toString());

	}
	 
	/**
	 * 根据sku获取sku详细信息
	 * @param sku
	 * @param username
	 * @param password
	 * @param response
	 */
	@RequestMapping(value = "skuDetailed", method = RequestMethod.POST)
	public void skuDetailed(@RequestParam(value = "sku") String sku,
			@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json = null;
		try {
			log.info("username=="+username);
			log.info("password=="+password);
			log.info("sku=="+sku);
			out = response.getWriter();
			json = new JSONObject();
			EdiGoodsSku ediGoodsSku=ediGoodsSkuService.findByCustomerCodeAndSku(username, sku);
			if(ediGoodsSku==null){
				json.put("status", "-1");
				json.put("content", "没有该商品信息");
				out.print(json.toString());
				return;
			}
			json.put("status", "1");
			json.put("content", ediGoodsSku);
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
		//log.info("skuDetailed=="+json.toString());

	}
	

	/**
	 * 根据订单号获取订单详细信息,获取一批订单信息
	 * @param ordenos
	 * @param username
	 * @param password
	 * @param response
	 */
	@RequestMapping(value = "ordenoDetaileds", method = RequestMethod.POST)
	public void ordenoDetaileds(@RequestParam(value = "ordenos") String ordenos,
			@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json = null;
		try {
			log.info("username=="+username);
			log.info("password=="+password);
			log.info("ordenos=="+ordenos);
			out = response.getWriter();
			json = new JSONObject();
			TMSSource source=ediSourceService.getEdiOrders(username,ordenos);//根据订单号获取订单详细信息
			JsonConfig jsonConfig = new JsonConfig();  
			jsonConfig.registerJsonValueProcessor(java.util.Date.class, new MyJsDateJsonValueProcessor());  
			json=JSONObject.fromObject(source, jsonConfig);
			out.print(json.toString());
		} catch (Exception e) {
			log.error("error", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
		//log.info("ordenoDetaileds=="+json.toString());
	}
	
	/**
	 * 根据订单号获取订单详细信息,获取单个订单信息
	 * @param ordenos
	 * @param username
	 * @param password
	 * @param response
	 */
	@RequestMapping(value = "ordenoDetailed", method = RequestMethod.POST)
	public void ordenoDetailed(@RequestParam(value = "ordenos") String ordeno,
			@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json = null;
		try {
			log.info("username=="+username);
			log.info("password=="+password);
			log.info("ordeno=="+ordeno);
			out = response.getWriter();
			json = new JSONObject();
			EdiOrder ediOrder=ediSourceService.getEdiOrder(username,ordeno);//根据订单号获取订单详细信息
			if(ediOrder==null){
				json.put("status", "-1");
				json.put("content", "没有该订单信息");
				out.print(json.toString());
				return;
			}
			JsonConfig jsonConfig = new JsonConfig();  
			jsonConfig.registerJsonValueProcessor(java.util.Date.class, new MyJsDateJsonValueProcessor());  
			JSONObject jsonTemp=JSONObject.fromObject(ediOrder, jsonConfig);
			json.put("status", "1");
			json.put("content", jsonTemp.toString());
			out.print(json.toString());
		} catch (Exception e) {
			log.error("error",e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
		//log.info("ordenoDetailed=="+json.toString());
	}
}
